package org.example.springboot.dao.impl;

import org.example.springboot.dao.SearchDAO;
import org.example.springboot.entity.Search;
import org.example.springboot.util.JDBCUtils;

import java.util.List;

public class SearchDAOImpl implements SearchDAO {
    @Override
    public Search selectBySerchWords(String search_words) {
        List<Search> searches = JDBCUtils.executeQuery(Search.class,"select * from s_search_history where search_words = ?",search_words);
        if (searches.isEmpty()){
            return null;
        }
        return searches.get(0);
    }

    @Override
    public Integer selectSearchCount() {
        return JDBCUtils.executeCount("select count(*) from s_search_history");
    }

    @Override
    public List<Search> selectSearchlimit(int start, int rows) {
        return JDBCUtils.executeQuery(Search.class,"select * from s_search_history limit ?,?",
                start,rows);
    }

    @Override
    public List<Search> selectAll() {
        List<Search> searches = JDBCUtils.executeQuery(Search.class,"select * from s_search_history");
        return searches;
    }

    @Override
    public Search selectById(String id) {
        List<Search> searches = JDBCUtils.executeQuery(Search.class,"select * from s_search_history where id = ?",id);
        if (searches.isEmpty()){
            return null;
        }
        return searches.get(0);
    }

    @Override
    public boolean insert(Search search) {
        int rows = JDBCUtils.executeUpdate("insert into s_search_history(id,search_words,num,search_time) values(?,?,?,?)",
                search.getId(),search.getSearch_words(),search.getNum(),search.getSearch_time());
        return rows > 0;
    }

    @Override
    public boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_search_history where id = ?",id);
        return rows > 0;
    }

    @Override
    public boolean update(Search search) {
        int rows = JDBCUtils.executeUpdate("update s_search_history set search_words=?,num=?,search_time=? where id=?",
                search.getSearch_words(),search.getNum(),search.getSearch_time(),search.getId());
        return rows > 0;
    }
}
